package cn.istartfinancial.config.flowable;

import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.job.service.impl.asyncexecutor.AsyncExecutor;
import org.flowable.spring.ProcessEngineFactoryBean;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.FlowableMailProperties;
import org.flowable.spring.boot.FlowableProperties;
import org.flowable.spring.boot.ProcessEngineAutoConfiguration;
import org.flowable.spring.boot.ProcessEngineServicesAutoConfiguration;
import org.flowable.spring.boot.app.FlowableAppProperties;
import org.flowable.spring.boot.idm.FlowableIdmProperties;
import org.flowable.spring.boot.process.FlowableProcessProperties;
import org.flowable.spring.boot.process.ProcessAsync;
import org.flowable.spring.boot.process.ProcessAsyncHistory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;

/**
 * @author: Nzp
 * @date: 2018/8/23
 * @description: flowable相关配置
 */
@Configuration
@AutoConfigureBefore({ProcessEngineServicesAutoConfiguration.class})
public class FlowableConfig extends ProcessEngineAutoConfiguration
{

    @Value("${dataType}") private String dataType;

    public FlowableConfig(FlowableProperties flowableProperties, FlowableProcessProperties processProperties,
                          FlowableAppProperties appProperties, FlowableIdmProperties idmProperties,
                          FlowableMailProperties mailProperties)
    {
        super(flowableProperties, processProperties, appProperties, idmProperties, mailProperties);
    }

    @Bean
    @Override
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(
            DataSource dataSource, PlatformTransactionManager platformTransactionManager,
            @ProcessAsync ObjectProvider<AsyncExecutor> asyncExecutorProvider,
            @ProcessAsyncHistory ObjectProvider<AsyncExecutor> asyncHistoryExecutorProvider)
    {
        SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
        configuration.setDataSource(dataSource);
        //表不存在创建表
        configuration.setDatabaseSchemaUpdate("true");
        //指定数据库类型
        configuration.setDatabaseType(dataType);
        //历史变量
        configuration.setHistory("full");
        //指定字体
        configuration.setActivityFontName("宋体");
        configuration.setAnnotationFontName("宋体");
        configuration.setLabelFontName("宋体");
        configuration.setTransactionManager(platformTransactionManager);
        return configuration;
    }

    @Bean
    public ProcessEngineFactoryBean processEngine(ProcessEngineConfiguration configuration)
    {
        ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
        processEngineFactoryBean.setProcessEngineConfiguration((ProcessEngineConfigurationImpl) configuration);
        return processEngineFactoryBean;
    }
}